<html >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
    <title>login</title>

    <link href="css/bootstrap/bootstrap.min.css" title="" rel="stylesheet" />
    <link title="orange" href="css/login.css" rel="stylesheet" type="text/css">

</head>
<body>

<div style="height:1px;"></div>
<div class="login">
    <header>
        <h1>登录</h1>
    </header>
    <div class="sr">
        <form method="post" action=" " id="loginForm">
            <div class="name">
                <input type="text"  placeholder="输入登录名" class="name_inp" id="username">
            </div>
            <div class="name">
                <input type="password"  placeholder="输入登录密码" class="name_inp" id="pwd_input">
            </div>
            <div class="dl" data-toggle="buttons-radio">
                <button type="submit" class="btn btn-primary">登录</button>
                <button type="button" class="btn btn-primary" onclick="window.location.href='register.html'">注册</button>
            </div>
        </form>
    </div>
  </div>
  <script src="js/jquery-2.1.1.min.js"></script>
  <script src="js/jquerysession.js"></script>
  <script>
      //首先先清除session里面的所有信息
      $(document).ready(function () {
          $.session._clearSession();
      });
       $(function () {
        $('#loginForm').submit(function (event) {
            event.preventDefault();
            //获取页面内容
            var username = $('#username').val();
            var password = $('#pwd_input').val();
            if (username.length == 0 || password.length == 0){
                alert("请输入用户名或密码");
                window.location.href="login.html";
            }else{
                //通过json将前端数据封装成类
                var user =JSON.stringify({"username":username,"password":password});
                console.log(user);

                // ajax传参
                $.ajax({
                    type:"post",
                    url:"/toLogin",
                    data:user,
                    // data:{"username":username,"password":password},
                    contentType:"application/json",
                    dataType:"json",
                    success: function (data) {
                        console.dir(data);
                        if (data.msg == '登录成功') {
                            console.dir(data.data.usertype);
                            //保存用户的用户id
                            $.session.set('userid',data.data.id);
                            console.log($.session.get('userid'));
                            $.session.set('usertype',data.data.usertype);
                            console.log($.session.get('usertype'));

                            //判断用户状态
                            if (data.data.state == 1){
                                alert("请等管理员确认之后再次登录！！！");
                                window.location.href="login.html";
                            }else if (data.data.state == 3){
                                alert("您已经被禁用，请及时联系管理员！！！");
                                window.location.href="login.html";
                            }else{
                                //用户类型 1->教师
                                if (data.data.usertype == 1){
                                    window.location.href="pages/teacher/tea_index.html";
                                }
                                //用户类型 2->学生
                                else if (data.data.usertype == 2){
                                    window.location.href="pages/student/stu_index.html";
                                }
                                //用户类型 3->管理员
                                else if (data.data.usertype == 3){
                                    window.location.href="pages/manager/adm_index.html";
                                }
                            }

                        }else{
                            //输出错误信息
                            alert(data.msg);
                            window.location.href="login.html";
                        }
                    },
                    error:function(data){
                        alert("请及时联系管理员");
                    }
                })
            }

        })
    })

  </script>
</body>
</html>

